package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.CommentMapper;
import cn.tedu.egweibo2.pojo.dto.CommentDTO;
import cn.tedu.egweibo2.pojo.entity.Comment;
import cn.tedu.egweibo2.pojo.vo.CommentListVO;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "03.评论模块")
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired(required = false)
    private CommentMapper commentMapper;

    /**
     * 发布评论功能
     * 接口地址:/v1/comment/insert
     * 请求方式:POST
     * 请求数据类型:application/json
     * 响应数据类型:int
     */
    @ApiOperation(value = "发布评论功能")
    @PostMapping("insert")
    public int insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userVO.getId());
        commentMapper.insertComment(comment);
        return 1;
    }

    /**
     * 获取评论功能
     * 接口地址:/v1/comment/selectByWeiboId
     * 请求方式:GET
     * 请求数据类型:application/x-www-form-urlencoded
     * 响应数据类型:见响应参数
     */
    @ApiOperation(value = "获取评论功能")
    @GetMapping("selectByWeiboId")
    public List<CommentListVO> selectByWeiboId(int id){
        List<CommentListVO> list = commentMapper.selectCommentList(id);
        return list;
    }
}
